package cn.heygod.nacostool.web;

import cn.heygod.nacostool.bean.Message;
import cn.heygod.nacostool.bean.PageData;
import cn.heygod.nacostool.client.NacosClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Simon
 */
@Controller
@RequestMapping("/api/instance")
public class InstanceController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private NacosClient nacosClient;


    @RequestMapping("/list")
    @ResponseBody
    public Object list(){
        List list = nacosClient.getInstancesAllServices();
        return new PageData(list);
    }


    @RequestMapping("/online")
    @ResponseBody
    public Object online(@RequestParam("id") String instanceId){
        Message message = new Message(true);
        try{
            nacosClient.online(instanceId);
            logger.info("online:{}",instanceId);
        }catch (Exception e){
            message.setSuccess(true);
        }
        return message;
    }


    @RequestMapping("/offline")
    @ResponseBody
    public Object offline(@RequestParam("id") String instanceId){
        Message message = new Message(true);
        try{
            nacosClient.offline(instanceId);
            logger.info("offline:{}",instanceId);
        }catch (Exception e){
            message.setSuccess(true);
        }
        return message;
    }
}
